package health.lm.com.mine.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.util.TimerHelper;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.present.SettingMobilePresenter;
import health.lm.com.router.ARouterConstant;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;


/**
 * 手机号
 * @author Administrator
 */
@Route(path = ARouterConstant.SettingMobileActivity)
public class SettingMobileActivity extends XActivity<SettingMobilePresenter> {

    @BindView(R2.id.title_bar)
    CommonTitleBar titleBar;
    @BindView(R2.id.tv_submit)
    TextView tvSubmit;
    @BindView(R2.id.tv_phone)
    EditText tvPhone;
    @BindView(R2.id.tv_code)
    EditText tvCode;
    @BindView(R2.id.tv_get_code)
    TextView tvGetCode;
    private CountDownTimer mTimerHelper;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting_mobile;
    }

    @Override
    public SettingMobilePresenter newP() {
        return new SettingMobilePresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {

        titleBar.setListener((v, action, extra) -> {
            if (action == ACTION_LEFT_BUTTON) {
                finish();
            }
        });

        //获取验证码
        RxView.clicks(tvGetCode).subscribe(v->{
            String phone = tvPhone.getText().toString().trim();
            if (!RegexUtils.isMobileExact(phone)) {
                getvDelegate().toastShort("请输入正确的手机号");
            }else {
                getP().getCode(phone);
                mTimerHelper = TimerHelper.countDown(tvGetCode,60000,1000,"获取验证码");
            }
        });
//        RxView.clicks(tvGetCode)
//                .compose(bindToLife())
//                .flatMap(o -> {
//                    String phone = tvPhone.getText().toString().trim();
//                    if (!RegexUtils.isMobileExact(phone)) {
//                        getvDelegate().toastShort("请输入正确的手机号");
//                        return Observable.empty();
//                    } else {
//                        getP().getCode(phone);
//                        return Observable.just(true);
//                    }
//                })
//                .flatMap(aBoolean -> {
//                    RxView.enabled(tvGetCode).accept(false);
//                    RxTextView.text(tvGetCode).accept("重新发送(60)s");
//                    return Observable.interval(0, 1, TimeUnit.SECONDS, Schedulers.io())
//                            .take(60)
//                            .compose(bindToLife())
//                            .map(aLong -> 60 - (aLong + 1));
//                })
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(aLong -> {
//                    if (aLong == 0) {
//                        RxView.enabled(tvGetCode).accept(true);
//                        RxTextView.text(tvGetCode).accept("获取验证码");
//                    } else {
//                        RxTextView.text(tvGetCode).accept("重新发送(" + aLong + "s)");
//                    }
//                });


        //实名信息
        RxView.clicks(tvSubmit)
                .throttleFirst(2, TimeUnit.SECONDS)
                .compose(bindToLife())
                .subscribe(o -> {
                    if (StringUtils.isEmpty(tvPhone.getText().toString()) || StringUtils.isEmpty(tvCode.getText().toString())) {
                        getvDelegate().toastShort("请填写信息完整后再提交");
                        return;
                    }
                    getP().submit(tvPhone.getText().toString(), tvCode.getText().toString());
                });
    }

    public void submitSuccess() {
        getvDelegate().toastShort("修改成功");
        setResult(ARouterConstant.SUCCESS, new Intent().putExtra(ARouterConstant.USER_NAME, ""));
        finish();
    }

    public void getCodeSuccess() {
        getvDelegate().toastShort("验证码发送成功");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mTimerHelper) mTimerHelper.cancel();
    }
}
